<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\ApiController;
use App\Models\Hotel;
use App\Models\Order;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class OrderController extends Controller
{
    public $paginate =  10;

    public function index(Request $request,Order $order)
    {
        $hotel_list = Hotel::list(null);
//        dd($hotel_list);
        return view('admin.order.index',['hotel_list'=>$hotel_list]);
    }

    /**订单详情的获取
     * @param Order $order
     * @param Request $request
     * @return mixed
     */
    public function detail(Order $order,Request $request)
    {
    	$id = $request->order_id;
    	if(empty($id)){
    	    return ApiController::response(null,ApiController::ERROR_CODE,'订单id不能为空');
        }
        $order -> order_user_info = true;
    	$order -> coupon_info = true;
    	$data = $order-> api_order_list(['orders.id'=>$id]);
    	$data = array_values($data);
    	$url = url()->full();
    	$data = $data[0];
    	if($data['express_no']){
    		$data['expree_url'] = $this->getExpressUrl($data['express_encode'],$data['express_no'],$url);
    	}
        return ApiController::response($data,ApiController::SUC_CODE,'获取详情成功');
    }

    public function getExpressUrl($express_encode,$express_no,$callbackurl)
    {
       $url = "https://m.kuaidi100.com/index_all.html?type={$express_encode}&postid={$express_no}&callbackurl={$callbackurl}";
       return $url;
    }

    /**ajax加载订单列表
     * @param Request $request
     * @param Order $order
     * @return mixed
     */
    public function ajax_list(Request $request,Order $order)
    {
        $data = $order->admin_order_list($this->paginate,$request->all(),$request->input('status',0));
        return ApiController::response($data,ApiController::SUC_CODE,'获取列表成功');
    }


    public function getExpress()
    {
        $data = DB::table('express_company')->get();
        if (empty($data)){
            return ApiController::response(null,200);
        }else{
            $data = $data->toArray();
            return ApiController::response($data);
        }
    }

    public function send(Request $request)
    {
        $input = $request->all();
        if (empty($input['order_sn'])){
            return ApiController::response(null,'-1','订单号不能为空');
        }
        if (empty($input['express_encode'])){
            return ApiController::response(null,'-2','物流公司编码不能为空');
        }
//        if (empty($input['express_company'])){
//            return ApiController::response(null,'-3','物流公司不能为空');
//        }
        if (empty($input['express_no'])){
            return ApiController::response(null,'-4','运单号不能为空');
        }
        $input = $request->input();
        $input['express_company'] = DB::table('express_company')->where('encode','=',$input['express_encode'])->first()->company;
        $flag  = Order::send($input);
        if ($flag>=0){
            return ApiController::response(null,ApiController::SUC_CODE,'修改成功');
        }else{
            return ApiController::response(null,ApiController::ERROR_CODE,'订单修改失败');
        }
    }


    public function complete(Request $request)
    {
        $input = $request->all();
        if (empty($input['order_sn'])){
            return ApiController::response(null,'-1','订单号不能为空');
        }

        if (isset($input['shipping_status'])&&$input['shipping_status']==5&&empty($input['reject_return_back'])){
            return ApiController::response(null,'-1','请填写拒绝退货理由');
        }

        if (isset($input['pay_status'])&&$input['pay_status']==5&&empty($input['reject_refund_reason'])){
            return ApiController::response(null,'-1','请填写拒绝退款理由');
        }

        $flag  = Order::complete($input);
        if ($flag>=0){
            return ApiController::response(null,ApiController::SUC_CODE,'修改成功');
        }else{
            return ApiController::response(null,ApiController::ERROR_CODE,'订单修改失败');
        }
    }






}
